package com.trg.flex
{
	import com.trg.flex.dt.DataTable;
	import com.trg.flex.frm.FormContainer;
	import com.trg.flex.frm.FormEvent;
	import com.trg.flex.messages.ErrorMessageConvertor;
	
	import mx.controls.Alert;
	
	/**
	 * This is an example of a helper class that takes care of a lot of common list/form event handling.
	 */
	public class EntityActionHelper
	{
		protected var entityDelegate:IEntityDelegate = FrameworkConfig.entityDelegate;

		protected var _form:FormContainer;
		
		protected var errorMessageConvertor:ErrorMessageConvertor = ErrorMessageConvertor.getInstance();
		
		/**
		 * The form listen to for commit.
		 */
		[Bindable]
		public function get form():FormContainer {
			return _form;
		}
		public function set form(value:FormContainer):void {
			if (_form != null) {
				_form.removeEventListener(FormEvent.COMMIT, commitHandler);	
				_form.removeEventListener(FormEvent.COMMIT_COMPLETE, commitCompleteHandler);
			}
			_form = value;
			if (_form != null) {
				_form.addEventListener(FormEvent.COMMIT, commitHandler);	
				_form.addEventListener(FormEvent.COMMIT_COMPLETE, commitCompleteHandler);
			}
		}
		
		/**
		 * The table to get selection from and refresh when needed.
		 */
		[Bindable]
		public var table:DataTable;
		
		/**
		 * Whether of not to display a confirm dialog before executing a requested delete.
		 */
		[Bindable]
		public var confirmDelete:Boolean = true;

		/**
		 * Delete the items that are currently selected in the table.
		 */
		public function deleteSelected():void {
			if (table == null) return;
			deleteItems(table.selectedItems);
		}
		
		/**
		 * Delete the item or items passed in. If items is an array, all will be deleted;
		 * otherwise the single item that is specified will be deleted.
		 */
		public function deleteItems(items:*):void {
			if(items == null || (items is Array && items.length == 0)) return;
			if (!(items is Array)) {
				items = [items];
			}
			if (confirmDelete) {
				var msg:String = items.length > 1 ? "Delete these items?" : "Delete this item?"
				FlexUtils.confirm(msg,"DELETE", Alert.YES | Alert.NO, null, deleteConfirmCallback, items);
			} else {
				deleteConfirmCallback(Alert.YES, items);
			}
		}
		
		protected function deleteConfirmCallback(result:*, items:Array):void {
			if (result == Alert.YES) {
				entityDelegate.deleteList(items).addResponder(new Callback(deleteCompleteHandler, deleteFailureHandler)).call();
			}
		}
		
		protected function deleteCompleteHandler(result:*=null):void {
			if (table != null) table.refresh();
		}
		
		protected function deleteFailureHandler(fault:Error):void {
			Alert.show("Items could not be deleted. " + errorMessageConvertor.getMessage(fault).text, "ERROR");
		}
		
		protected function commitHandler(event:FormEvent):void {
			if (event.original == null)
				entityDelegate.create(event.formBean).addResponder(event.callback).call();
			else
				entityDelegate.update(event.formBean).addResponder(event.callback).call();
		}
		
		protected function commitCompleteHandler(event:FormEvent):void {
			if (table != null) table.refresh();
		}
	}
}